package logic

import (
	"context"
	"fmt"
	"meituan-waimai/models/mysql"

	"meituan-waimai/meituanrpc/internal/svc"
	"meituan-waimai/meituanrpc/meituanrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderCompleteLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderCompleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderCompleteLogic {
	return &OrderCompleteLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderCompleteLogic) OrderComplete(in *meituanrpc.OrderCompleteRequest) (*meituanrpc.OrderCompleteResponse, error) {
	// todo: add your logic here and delete this line
	// 骑手点击完成，推送订单状态至完成
	var order mysql.Order
	mysql.DB.Where("id = ?", in.OrderId).First(&order)
	if order.ID == 0 {
		return nil, fmt.Errorf("订单不存在")
	}

	order.OrderStatus = "已完成"
	mysql.DB.Updates(&order)
	return &meituanrpc.OrderCompleteResponse{}, nil
}
